package com.lv.common.widget.keyboard;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.util.AttributeSet;

import com.blankj.utilcode.util.SizeUtils;
import com.lv.common.R;

import java.util.List;

/**
 * 作者： ygx
 * 创建日期：2023/8/8 12:36 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */
public class CustomKeyboardView extends KeyboardView {

    private int mPressedKeyIndex = -1;
    private Drawable mNormalKeyBackground;
    private Drawable mPressedKeyBackground;
    private int mNormalTextColor;
    private int mPressedTextColor;
    public CustomKeyboardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        // 初始化正常状态和按下状态的背景图片
        Resources res = getResources();
        mNormalKeyBackground = res.getDrawable(R.drawable.bg_cir_white_4);
        mPressedKeyBackground = res.getDrawable(R.drawable.bg_theme_4);
        // 初始化正常状态和按下状态的字体颜色
        mNormalTextColor = res.getColor(R.color.color_242424);
        mPressedTextColor = res.getColor(R.color.colorWhite);
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Keyboard keyboard = getKeyboard();
        if (keyboard == null) {
            return;
        }

        List<Keyboard.Key> keys = keyboard.getKeys();
        for (Keyboard.Key key : keys) {
            if (key.pressed) {
                if (mPressedKeyIndex != key.codes[0]) {
                    // 按键被按下，记录按下的按键索引
                    mPressedKeyIndex = key.codes[0];
                    invalidate();
                }
                drawKeyBackground(canvas, key, mPressedKeyBackground); // 按下时的背景色
                drawTextColor(canvas, key, mPressedTextColor); // 按下时的字体颜色
            } else if (key.codes[0] == mPressedKeyIndex) {
                // 按键被释放，恢复正常状态
                mPressedKeyIndex = -1;
                invalidate();
                drawKeyBackground(canvas, key, mNormalKeyBackground); // 正常状态的背景色
                drawTextColor(canvas, key, mNormalTextColor); // 正常状态的字体颜色
            }
        }
    }

    private void drawKeyBackground(Canvas canvas, Keyboard.Key key, Drawable background) {
        background.setBounds(key.x, key.y, key.x + key.width, key.y + key.height);
        background.draw(canvas);
    }

    private void drawKeyBackground(Canvas canvas, Keyboard.Key key, int color) {
        Rect bounds = new Rect(key.x, key.y, key.x + key.width, key.y + key.height);
        Paint backgroundPaint = new Paint();
        backgroundPaint.setColor(color);
        canvas.drawRect(bounds, backgroundPaint);
    }
    private void drawTextColor(Canvas canvas, Keyboard.Key key, int color) {
        if (key.label != null) {
            Paint paint = new Paint();
            paint.setColor(color);
            paint.setTextSize(SizeUtils.dp2px(20.0f));
            paint.setTextAlign(Paint.Align.CENTER);
            canvas.drawText(key.label.toString(), key.x + (key.width / 2),
                    key.y + (key.height / 2), paint);
        }
    }
}
